<?
require_once('functions.php');

function GetExperiments()
{
	$connect = GetConnection();
	return $connect->query('SELECT * FROM Psi_Experiments');
}
function GetExperiment($id)
{
	$connect = GetConnection();
	$sql = "SELECT * FROM Psi_Experiments WHERE id=$id";
	$results = $connect->query($sql);
	//echo $sql; die();
	$row = $results->fetch_assoc();
	$connect->close();
	return $row;
}
function BlankExperiment()
{
	return array('Name'=>null,'Date'=>null,'Abstract'=>null,'Method'=>null,
		'PNumMale'=>null,'PNumFemale'=>null,'PAge'=>null,'PDemographicDetails'=>null,'PSelectionCriteria'=>null,
		'PAssignmentCriteria'=>null,'Conclusion'=>null,'Implications'=>null,'Findings'=>null,
		'created_at'=>null,'updated_at'=>null,'id'=>null);
}
function SaveExperiment($row)
{
	$connect = GetConnection();
	$escape = EscapeRow($row, $connect);
	$sql = 	"UPDATE Psi_Experiments "
		.	"Set"
		
		.	" Name='$escape[Name]',"
		.	" Date='$escape[Date]',"
		.	" Abstract='$escape[Abstract]',"
		.	" Method='$escape[Method]',"
		.	" PNumMale='$escape[PNumMale]',"
		.	" PNumFemale='$escape[PNumFemale]',"
		.	" PAge='$escape[PAge]',"
		.	" PDemographicDetails='$escape[PDemographicDetails]',"
		.	" PSelectionCriteria='$escape[PSelectionCriteria]',"
		.	" PAssignmentCriteria='$escape[PAssignmentCriteria]',"
		.	" Conclusion='$escape[Conclusion]',"
		.	" Implications='$escape[Implications]',"
		.	" Findings='$escape[Findings]' "
						
		.	"WHERE id=$row[id] ";
	$results = $connect->query($sql);
	// echo $sql; die();
	$error = $connect->error;
	$connect->close();
	
	return $error == '' ? TRUE : array('SQL Error' => $error);
}
function CreateExperiment($row) {
	$connect = GetConnection();
	$escape = EscapeRow($row, $connect);
	
	$Name 					= 	$_POST['Name'];
	$Date 					= 	$_POST['Date'];
	$Abstract 				= 	$_POST['Abstract'];
	$Method 				= 	$_POST['Method'];
	$PNumMale 				= 	$_POST['PNumMale'];
	$PNumFemale 			= 	$_POST['PNumFemale'];
	$PAge 					= 	$_POST['PAge'];
	$PDemographicDetails 	= 	$_POST['PDemographicDetails'];
	$PSelectionCriteria 	= 	$_POST['PSelectionCriteria'];
	$PAssignmentCriteria 	= 	$_POST['PAssignmentCriteria'];
	$Conclusion 			= 	$_POST['Conclusion'];
	$Implications 			= 	$_POST['Implications'];
	$Findings				= 	$_POST['Findings'];
		
	$sql = 
		"INSERT INTO Psi_Experiments (
			Name, Date, Abstract, Method, 
			PNumMale, PNumFemale, PAge, 
			PDemographicDetails, PSelectionCriteria, PAssignmentCriteria, 
			Conclusion, Implications, Findings, created_at
		) VALUES (
			'$escape[Name]', '$escape[Date]', '$escape[Abstract]', '$escape[Method]', 
			'$escape[PNumMale]', '$escape[PNumFemale]', '$escape[PAge]', 
			'$escape[PDemographicDetails]', '$escape[PSelectionCriteria]', '$escape[PAssignmentCriteria]', 
			'$escape[Conclusion]', '$escape[Implications]', '$escape[Findings]', NOW() 
		)";
	
	$result = $connect->query($sql);
	if ($result) {
		// Success!
		header("Location: index.php");
		exit;
	} else {
		// Display error message.
		echo "<p class=\"error\">Subject creation failed.</p>";
		echo "<p class=\"error\">" . mysql_error() . "</p>";
	}

	$error = $connect->error;
	$connect->close();
	
	return $error == '' ? TRUE : array('SQL Error' => $error);
}
function DeleteExperiment($id)
{
	$conn = GetConnection();
	$sql = 	"DELETE FROM Psi_Experiments WHERE id=$id";
	$results = $conn->query($sql);
	//echo $sql;
	$error = $conn->error;
	$conn->close();
	
	return $error == '' ? true : array('SQL Error' => $error);
}

/*function DeleteExperiment($row) {
	$connect = GetConnection();

	if (intval($_GET['id']) == 0) {
			header("Location: index.php");
			die();
	}
	
	$id = mysql_prep($_GET['id']);
	
	if ($subject = GetExperiment($id)) {
		
		$sql = "DELETE FROM Psi_Experiments WHERE id = {$id} LIMIT 1";
		$result = $connect->query($sql);
		if ( $connect->affected_rows == 1 ) {
			header("Location: index.php");
			die();
		} else {
			// Deletion Failed
			echo "<p class=\"error\">Subject deletion failed.</p>";
			echo "<p class=\"error\">" . mysql_error() . "</p>";
			echo "<a href=\"index.php\">Return to Main Page</a>";
		}
	} else {
		// Theorist didn't exist in database
			header("Location: index.php");
			die();
	}

	$connect->close();
}*/
function ValidateExperiment($row)
{
	$errors = array();
	if(empty($row['Name'])) $errors['Name'] = 'Name is required';
	if(empty($row['Date'])) $errors['Date'] = 'Date is required';
	if(empty($row['Abstract'])) $errors['Abstract'] = 'Abstract Year is required';
	if(empty($row['Method'])) $errors['Method'] = 'More Details Required';
	if(empty($row['PNumMale'])) $errors['PNumMale'] = 'If unknown put N/A';
	if(empty($row['PNumFemale'])) $errors['PNumFemale'] = 'If unknown put N/A';
	if(empty($row['PAge'])) $errors['PAge'] = 'Participants\'s age range required';
	if(empty($row['PDemographicDetails'])) $errors['PDemographicDetails'] = 'Demographics needed';
	if(empty($row['PSelectionCriteria'])) $errors['PSelectionCriteria'] = 'How were the Participants\'s selected?';
	if(empty($row['PAssignmentCriteria'])) $errors['PAssignmentCriteria'] = 'How where the Participants\'s assigned to each group?';
	if(empty($row['Conclusion'])) $errors['Conclusion'] = 'Also called Discussion';
	if(empty($row['Implications'])) $errors['Implications'] = 'What other somethings mentioned that this experiment can do better?';
	if(empty($row['Findings'])) $errors['Findings'] = 'What did the researcher find?';

	return count($errors) == 0 ? true : $errors;
}